<?php
/**
 * 参数设置模块
 * User: 毛文科
 * Date: 2019/3/20 0020
 * Time: 17:34
 */
namespace mwechat\src;
use think\Db;
use think\Exception;

class MaoWeiXin
{
    protected $option = [];
    //配置参数
    public function __construct($config=[])
    {
        $mokuai = request()->module();
        if(!$config){
            $config = Db::name($mokuai.'_wechats')->where('status',1)->find();
        }
        foreach ($config as $key=>$value){
            $this->option[$key] = $value;
        }
//        if(!isset($this->option['appid'])||!$this->option['appid']){
//            throw new Exception('请配置appid');
//        }
        return $this;
    }
    //获取appid
    public function getAppid(){
        if(isset($this->option['appid'])&&$this->option['appid']){
            return $this->option['appid'];
        }
        throw new Exception('没有设置appid');
    }

    /**设置appid
     * @param appid $appid
     * @return $this
     */
    public function setAppid($appid){
        $this->option['appid'] = $appid;
        return $this;
    }

    /**设置微信appsecret
     * @param 微信appsecret $appsecret
     * @return $this
     */
    public function setAppSecret($appsecret){
        $this->option['appsecret'] = $appsecret;
        return $this;
    }

    /**获取appsecret
     * @return mixed
     * @throws Exception
     */
    public function getAppSecret(){
        if(isset($this->option['appsecret'])&&$this->option['appsecret']){
            return $this->option['appsecret'];
        }
        throw new Exception('没有设置appsecret');
    }
    //获取商户id
    public function getMchId(){
        if(isset($this->option['mch_id'])&&$this->option['mch_id']){
            return $this->option['mch_id'];
        }
        throw new Exception('没有设置商户id');
    }

    /**设置商户id
     * @param 商户id $mch_id
     * @return $this
     */
    public function setMchId($mch_id){
        $this->option['mch_id'] = $mch_id;
        return $this;
    }

    /**获取商户key
     * @return mixed
     */
    public function getMchKey(){
        if(isset($this->option['mch_key'])&&$this->option['mch_key']){
            return $this->option['mch_key'];
        }
        throw new Exception('没有设置商户key');
    }

    /**设置商户key
     * @param 商户key $mch_key
     * @return $this
     */
    public function setMchKey($mch_key){
        $this->option['mch_key'] = $mch_key;
        return $this;
    }

    /**获取签名类型
     * @return mixed
     * @throws Exception
     */
    public function getSignType(){
        if(isset($this->option['sign_type'])&&$this->option['sign_type']){
            return $this->option['sign_type'];
        }
        throw new Exception('没有设置sign_type');
    }

    /**设置签名类型
     * @param 签名类型 $sign_type
     * @return $this
     */
    public function setSignType($sign_type){
        $this->option['sign_type'] = $sign_type;
        return $this;
    }

    /**设置支付回调路径
     * @param 支付回调路径 $notify_url
     * @return $this
     */
    public function setNotifyUrl($notify_url){
        $this->option['notify_url'] = $notify_url;
        return $this;
    }

    /**获取支付回调
     * @return mixed
     * @throws Exception
     */
    public function getNotifyUrl(){
        if(isset($this->option['notify_url'])&&$this->option['notify_url']){
            return $this->option['notify_url'];
        }
        throw new Exception('没有设置支付回调');
    }

    /**设置交易币种
     * @param 交易币种 $fee_type
     * @return $this
     */
    public function setFeeType($fee_type){
        $this->option['fee_type'] = $fee_type;
        return $this;
    }

    /**设置openid
     * @param openid $openid
     * @return $this
     */
    public function setOpenId($openid){
        $this->option['openid'] = $openid;
        return $this;
    }

    /**获取openid
     * @return mixed
     * @throws Exception
     */
    public function getOpenId(){
        if(isset($this->option['openid'])&&$this->option['openid']){
            return $this->option['openid'];
        }
        throw new Exception('没有设置openid');
    }

    /**设置支付金额
     * @param 支付金额 $total_fee
     * @return $this
     */
    public function setTotalFee($total_fee){
        $this->option['total_fee'] = $total_fee;
        return $this;
    }

    /**获取金额
     * @return mixed
     * @throws Exception
     */
    public function getTotalFee(){
        if(isset($this->option['total_fee'])&&$this->option['total_fee']){
            return $this->option['total_fee'];
        }
        throw new Exception('没有设置支付金额');
    }

    /**设置订单号
     * @param 定单号 $out_trade_no
     * @return $this
     */
    public function setOutTradeNo($out_trade_no){
        $this->option['out_trade_no'] = $out_trade_no;
        return $this;
    }
    /**获取订单号
     * @return mixed
     * @throws Exception
     */
    public function getOutTradeNo(){
        if(isset($this->option['out_trade_no'])&&$this->option['out_trade_no']){
            return $this->option['out_trade_no'];
        }
        throw new Exception('没有设置out_trade_no');
    }

    /**设置商品id
     * @param 商品id $product_id
     * @return $this
     */
    public function setProductId($product_id){
        $this->option['product_id'] = $product_id;
        return $this;
    }

    /**获取商品id
     * @return mixed
     * @throws Exception
     */
    public function getProductId(){
        if(isset($this->option['product_id'])&&$this->option['product_id']){
            return $this->option['product_id'];
        }
        throw new Exception('没有设置商品id');
    }

    /**设置商品描述
     * @param  商品描述 $body
     * @return $this
     */
    public function setBody($body){
        $this->option['body'] = $body;
        return $this;
    }

    /**获取商品描述
     * @return mixed
     * @throws Exception
     */
    public function getBody(){
        if(isset($this->option['body'])&&$this->option['body']){
            return $this->option['body'];
        }
        throw new Exception('没有设置body');
    }

    /**设置商品详情
     * @param 商品详情 $detail
     * @return $this
     */
    public function setDetail($detail){
        $this->option['detail'] = $detail;
        return $this;
    }

    /**获取商品详情
     * @return mixed
     * @throws Exception
     */
    public function getDetail(){
        if(isset($this->option['detail'])&&$this->option['detail']){
            return $this->option['detail'];
        }
        throw new Exception('没有设置商品详情');
    }

    /**设置附加数据
     * @param 附加数据 $attach
     * @return $this
     */
    public function setAttach($attach){
        $this->option['attach'] = $attach;
        return $this;
    }

    /**获取附加数据
     * @return mixed
     * @throws Exception
     */
    public function getAttach(){
        if(isset($this->option['attach'])&&$this->option['attach']){
            return $this->option['attach'];
        }
        throw new Exception('获取附加数据');
    }

    /**设置随机字符串
     * @param 随机字符串 $nonce_str
     * @return $this
     */
    public function setNonceStr($nonce_str){
        $this->option['nonce_str'] = $nonce_str;
        return $this;
    }

    /**获取随机字符串
     * @return mixed
     * @throws Exception
     */
    public function getNonceStr(){
        if(isset($this->option['nonce_str'])&&$this->option['nonce_str']){
            return $this->option['nonce_str'];
        }
        throw new Exception('没有设置随机字符串');
    }
    /**获取交易币种
     * @return mixed
     * @throws Exception
     */
    public function getFeeType(){
        if(isset($this->option['fee_type'])&&$this->option['fee_type']){
            return $this->option['fee_type'];
        }
        throw new Exception('没有设置交易币种');
    }

    /**设置获取code回调地址
     * @param  获取code回调地址 $redirect_uri
     * @return $this
     */
    public function setRedirectUri($redirect_uri){
        $this->option['redirect_uri'] = $redirect_uri;
        return $this;
    }
    /**获取获取code回调地址
     * @return mixed
     * @throws Exception
     */
    public function getRedirectUri(){
        if(isset($this->option['redirect_uri'])&&$this->option['redirect_uri']){
            return $this->option['redirect_uri'];
        }
        throw new Exception('没有设置获取code回调');
    }

    /**设置access_token
     * @param access_token $access_token
     * @return $this
     */
    public function setAccessToken($access_token){
        $this->option['access_token'] = $access_token;
        return $this;
    }

    /**获取access_token
     * @return mixed
     * @throws Exception
     */
    public function getAccessToken(){
        if(isset($this->option['access_token'])&&$this->option['access_token']){
            return $this->option['access_token'];
        }
        throw new Exception('没有设置access_token');
    }

    /**设置终端ip
     * @param 终端ip $spbill_create_ip
     * @return $this
     */
    public function setSpbillCreateIp($spbill_create_ip){
        $this->option['spbill_create_ip'] = $spbill_create_ip;
        return $this;
    }

    /**获取终端ip
     * @return mixed
     * @throws Exception
     */
    public function getSpbillCreateIp(){
        if(isset($this->option['spbill_create_ip'])&&$this->option['spbill_create_ip']){
            return $this->option['spbill_create_ip'];
        }
        throw new Exception('没有设置终端ip');
    }

    /**设置支付交易类型
     * @param 支付类型 $trade_type
     * @return $this
     */
    public function setTradeType($trade_type){
        $this->option['trade_type'] = $trade_type;
        return $this;
    }

    /**获取支付交易类型
     * @return mixed
     * @throws Exception
     */
    public function getTradeType(){
        if(isset($this->option['trade_type'])&&$this->option['trade_type']){
            return $this->option['trade_type'];
        }
        throw new Exception('没有设置支付签名类型');
    }

    /**微信小程序设置grant_type
     * @param $grant_type
     * @return $this
     */
    public function setGrantType($grant_type){
        if($grant_type){
            $this->option['grant_type'] = $grant_type;
        }
        return $this;
    }

    /**获取小程序grant_type
     * @return mixed
     * @throws Exception
     */
    public function getGrantType(){
        if(isset($this->option['grant_type'])&&$this->option['grant_type']){
            return $this->option['grant_type'];
        }
        throw new Exception('没有设置小程序grant_type');
    }

}